<template>
  <div>
    <el-table :data="adminList" border style="width: 100%">
      <el-table-column prop="uid" label="用户编号" width="180">
      </el-table-column>
      <el-table-column prop="username" label="用户名称" width="180">
      </el-table-column>
      <el-table-column prop="rolename" label="所属角色" width="180">
      </el-table-column>

      <el-table-column prop="status" label="状态">
        <template slot-scope="item">
          <div>
            <el-tag effect="dark" type="success" v-if="item.row.status == 1">
              启用
            </el-tag>
            <el-tag effect="dark" type="warning" v-else> 禁用 </el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="edit(item.row)"
            ></el-button>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-delete"
              circle
              @click="del(item.row.uid)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="changepage"
      :page-size="pageSize"
      :total="usertotal"
    >
    </el-pagination>
  </div>
</template>

<script>
import { userdelete } from "../../require/api";
//引入vuex
import { mapActions, mapGetters, mapState } from "vuex";
export default {
  data() {
    return {};
  },
  mounted() {
    this.getAdminAction();
    this.getTotalAction();
  },
  computed: {
    ...mapGetters({
      adminList: "admin/getAdmin",
      usertotal: "admin/getUserTotal",
      pageSize: "admin/getSize",
    }),
    // ...mapState("admin", ["usertotal"]),
    // 获取管理员列表（分页）
  },
  methods: {
    ...mapActions({
      getAdminAction: "admin/getAdminAction",
      getTotalAction: "admin/getTotalAction",
      changePageAction: "admin/changePageAction",
    }),
    // 分页   当前页
    changepage(e) {
      // console.log(e, "page");
      this.changePageAction(e);
    },

    // 编辑
    edit(e) {
      // console.log(e);
      this.$emit("edit", e);
    },
    // 删除
    del(uid) {
      // console.log(uid);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 请求接口
          userdelete({ uid }).then((res) => {
            // console.log(res);
            if (res.code == 200) {
              // 更新页面
              this.getAdminAction();
              this.getTotalAction();
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang=less scoped >
.el-table {
  min-height: 400px;
}
.el-pagination {
  margin-top: 15px;
  float: right;
}
</style>
